;
(function(factory) {
	layui.use(['layer', 'laypage', 'table', 'form'], function() {
		factory(jQuery, window, document, layui);
	});
}(function($, window, document) {

	$(function() {
		funOp.init();
	});

	var funOp = {
		loginUser: $.fn.commUtil.getLoginUser(),
		init: function() {
			funOp.queryMenu();
			$("#username").text(funOp.loginUser.username)
		},

		queryMenu: function() {
			var loginUser = funOp.loginUser;
			var s =
				'SELECT a.* from menu a LEFT JOIN role_menu b on (a.id = b.menu_id) where a.del_flag = 0 and b.role_id = ? ;';
			var obj = {
				sql: $.fn.commUtil.getSqlStr(s, [loginUser.roleId])
			}

			$.newAjax({
				url: $.fn.commUtil.constant.baseUrl + '/dealSql',
				data: JSON.stringify(obj),
				success: function(res) {
					console.info(res)
					if (res.code != 0) {
						alert(res.msg);
						return;
					}

					funOp.dealMenu(res.data);

				}
			});
		},

		dealMenu: function(list) {
			var level1 = [];
			$.each(list, function(i, n) {
				if (n.level == 1) {
					level1.push(n);
				}
			});
			level1.sort(function(a, b) {
				return a.orderIndex - b.orderIndex;
			});
			var newLevel1 = [];
			$.each(level1, function(i, n) {
				n.children = funOp.getChildren(n.id, list);
				n.children.sort(function(a, b) {
					return a.orderIndex - b.orderIndex;
				});
				newLevel1.push(n);
			})
			var getTpl = demo.innerHTML;
			var view = document.getElementById('nav');
			layui.laytpl(getTpl).render(level1, function(html) {
				view.innerHTML = html;
			});
		},
		getChildren: function(parentId, menuList) {
			var data = [];
			var treeObj;
			for (var i = 0; i <= menuList.length - 1; i++) {
				if (parentId == menuList[i].parentId) {
					data.push(menuList[i]);
				}
			}
			return data;
		},

	};

}));
